﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace aspnet2.User
    {
    public partial class Mainpage : System.Web.UI.Page
        {
        [Serializable()]
        public class ShoppingItem
            {
            public int ProductID { get; set; }
            public int Quantity { get; set; }
            public string Color { get; set; }
            public DateTime CallTime { get; set; }

            }

        public List<ShoppingItem> ShoppingCart = new List<ShoppingItem>();
        protected void Page_Load(object sender, EventArgs e)
            {


            if (!IsPostBack)
                if (HttpContext.Current.User.IsInRole("Manager"))
                    {
                    this.LinkButton1.Visible = true;
                    this.ErrorInfo.Text = null;
                    }


            }


        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
            {
            try
                {
                ((Panel)this.GridView1.Rows[e.NewSelectedIndex].FindControl("Panel1")).Visible = true;
                }
            catch (Exception ex)
                {

                this.ErrorInfo.Text = ex.Message;
                }
            }

        protected void Button2_Click(object sender, EventArgs e)
            {

            Page.Validate();
            try
                {

                if (!IsValid) { this.ErrorInfo.Text = "验证失败"; }
                else
                    {

                    ShoppingItem OneShoppingItem = new ShoppingItem();

                    OneShoppingItem.ProductID = Int32.Parse
                        (

                                (
                                    (Label)
                                        (
                                            (Button)sender
                                        ).NamingContainer.FindControl("ItemID")
                                ).Text

                        );
                    OneShoppingItem.Quantity = Int32.Parse
                        (
                        (
                            (TextBox)
                                (
                                  (Button)sender
                                ).NamingContainer.FindControl("QuantityBox")
                        ).Text
                        );
                    OneShoppingItem.Color = ((DropDownList)((Button)sender).NamingContainer.FindControl("InkColor")).Visible == true ? ((DropDownList)((Button)sender).NamingContainer.FindControl("InkColor")).SelectedValue : null;
                    OneShoppingItem.CallTime = DateTime.Now;


                    if (Session["ShoppingCart"] != null)
                        {
                        ShoppingCart = (List<ShoppingItem>)Session["ShoppingCart"];
                        //相隔太近时间购买则失败
                        var temp = from a in ShoppingCart
                                   where a.CallTime.CompareTo(OneShoppingItem.CallTime) == 0
                                   select a;
                        if (temp.ToArray().Count() > 0) throw new Exception("买得太快了，请休息一下！");

                        }
                    //允许一个物品多次购买
                    ShoppingCart.Add(OneShoppingItem);

                    Session["ShoppingCart"] = ShoppingCart;
                    ((Label)((Button)sender).NamingContainer.FindControl("Label4")).Text = "购买成功";
                    ((Label)((Button)sender).NamingContainer.FindControl("Label4")).Visible = true;


                    }

                }
            catch (Exception ex)
                {
                this.ErrorInfo.Text = ex.Message;

                }
            }

        protected void Button4_Click(object sender, EventArgs e)
            {


            Response.Redirect("~/User/Clearing.aspx");

            }

        protected void Button5_Click(object sender, EventArgs e)
            {
            try
                {
                ((TextBox)((Button)sender).NamingContainer.FindControl("QuantityBox")).Focus();
                foreach (GridViewRow r in GridView1.Rows)
                    {
                    ((Panel)r.FindControl("Panel1")).Visible = false;

                    }
                ((Panel)((Button)sender).NamingContainer.FindControl("Panel1")).Visible = true;
                if (((int)GridView1.DataKeys[((GridViewRow)((Button)sender).NamingContainer).RowIndex].Value) == 1)
                    ((DropDownList)((Button)sender).NamingContainer.FindControl("InkColor")).Visible = true;
                }
            catch (Exception ex)
                {

                this.ErrorInfo.Text = ex.Message;
                }
            }

        protected void Button6_Click(object sender, EventArgs e)
            {
            ((Panel)((Button)sender).NamingContainer.FindControl("Panel1")).Visible = false;
            }

        protected void Button3_Click(object sender, EventArgs e)
            {



            }



        protected void QuantityBox_TextChanged(object sender, EventArgs e)
            {

            }

        protected void UserSpace_Click(object sender, EventArgs e)
            {
            Response.Redirect("~/User/UserProfile.aspx");
            }
        protected void LinkButton1_Click(object sender, EventArgs e)
            {
            Response.Redirect("~/Manager/ManagerProfile.aspx");
            }

        protected void LinkButton2_Click(object sender, EventArgs e)
            {
            Response.Redirect("~/User/UserProfile.aspx");
            }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
            {
            try
                {
                ((TextBox)((ImageButton)sender).NamingContainer.FindControl("QuantityBox")).Focus();
                foreach (GridViewRow r in GridView1.Rows)
                    {
                    ((Panel)r.FindControl("Panel1")).Visible = false;

                    }
                ((Panel)((ImageButton)sender).NamingContainer.FindControl("Panel1")).Visible = true;
                if (((int)GridView1.DataKeys[((GridViewRow)((ImageButton)sender).NamingContainer).RowIndex].Value) == 1)
                    ((DropDownList)((ImageButton)sender).NamingContainer.FindControl("InkColor")).Visible = true;
                }
            catch (Exception ex)
                {

                this.ErrorInfo.Text = ex.Message;
                }
            }










        }
    }
